<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>WeakMap Private Members</title>
</head>
<body>
  <script>
    const User = (() => {
      const wm = new WeakMap();

      class User {
        constructor(id) {
          this.idProperty = Symbol('id');
          this.setId(id);
        }

        setPrivate(property, value) {
          const privateMembers = wm.get(this) || {};
          privateMembers[property] = value;
          wm.set(this, privateMembers);
        }

        getPrivate(property) {
          return wm.get(this)[property];
        }

        setId(id) {
          this.setPrivate(this.idProperty, id);
        }

        getId() {
          return this.getPrivate(this.idProperty);
        }
      }
      return User;
    })();


    const user = new User(123);
    console.log(user.getId());  // 123
    user.setId(456);
    console.log(user.getId());  // 456
  </script>
</body>
</html>